package com.dangdi.show.controller;

import com.aliyuncs.exceptions.ClientException;
import com.dangdi.show.dao.ShiroDao;
import com.dangdi.show.dao.SmsDemo;
import com.dangdi.show.service.ManagerService;
import com.dangdi.show.service.ShiroService;
import com.dangdi.show.service.WechatUserService;
import com.dangdi.show.util.WechatApiUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Resource
    WechatUserService wechatUserService;
    @Resource
    ShiroService shiroService;
    @Resource
    ManagerService managerService;
    @Resource
    ShiroDao shiroDao;

    //管理员登录
    @ResponseBody
    @RequestMapping(value = "managerLogin")
    public Map<String, Object> managerLogin(HttpServletRequest request, ModelMap mp, String manager_tel, String manager_password, String rememberme) {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean rembme = true;
        if (rememberme == null) {
            rembme = false;
        } else {
            rembme = rememberme.equals("on") ? true : false;
        }
        if (manager_tel == null || manager_tel.equals("")) {
            map.put("statusCode", "-1");
            map.put("message", "请输入手机号码");
            return map;
        }
        if (manager_password == null || manager_password.equals("")) {
            map.put("statusCode", "-1");
            map.put("message", "请输入密码");
            return map;
        }
        Map<String, Object> manager = managerService.ManagerLogin(manager_tel, manager_password);
        if (manager == null) {
            map.put("statusCode", "-1");
            map.put("message", "手机号码或密码错误");
            return map;
        }
        if (manager.get("manager_status").toString().equals("-1")) {
            map.put("statusCode", "-1");
            map.put("message", "帐号异常，请联系管理员");
            return map;
        }
        HttpSession session = request.getSession();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(manager_tel, manager_password, rembme);
        try {
            subject.login(token);
            map.put("statusCode", "0");
            map.put("message", "登录成功");
            session.setAttribute("manager", manager);
            mp.addAttribute("manager", manager);
            return map;
        } catch (AuthenticationException e) {
            map.put("statusCode", "-1");
            map.put("message", "手机号码或密码错误");
            return map;
        }

    }

    //生成页面消息
    public String alertMessage(String msg) {
        String m = "<script>layer.alert('" + msg + "');</script>";
        return m;
    }

    //发送验证码
    @ResponseBody
    @RequestMapping("sendPhoneCode")
    public Map<String, Object> managerZhucePhoneCode(HttpServletRequest request, String manager_tel) throws ClientException, InterruptedException {
        Map<String, Object> map = new HashMap<String, Object>();
        if (manager_tel == null || manager_tel.equals("")) {
            map.put("statusCode", "-1");
            map.put("message", "请填写完整的手机号码");
            return map;
        } else {
            HttpSession session = request.getSession();
//            getPhoneCode phoneCode = new getPhoneCode();
            String code = getCode();
            System.out.println(code);
            Map m = new HashMap();
            m.put("code", code);
            SmsDemo.sendSms(manager_tel, m, SmsDemo.MSG_TO_USER_MESSAGE_CODE);
            session.setAttribute("phoneCode", code);
            session.setAttribute("phoneCodePhone", manager_tel);
            map.put("statusCode", "0");
            map.put("message", "验证码已发送");
            return map;
        }


    }

    //生成验证码
    public String getCode() {
        Random ran = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            code = code + ran.nextInt(10);
        }
        return code;
    }

    @RequestMapping("managerLoginOut")
    public String ManagerLoginOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "index";
    }

    @ResponseBody
    @RequestMapping("Wechatlogin")
    public Map<String, Object> Wechatlogin(HttpServletRequest request, String code) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> xmlMap = new HashMap<String, String>();
        Map<String, Object> data = new HashMap<String, Object>();
        WechatApiUtil wechatApiUtil = new WechatApiUtil();
        String openID = wechatApiUtil.getOpenId(code);
        if (wechatUserService.FindOpenIdByOpenId(openID) == null) {
            wechatUserService.InserOpenId(openID);
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(openID, openID, true);
        subject.login(token);
        request.getSession().setAttribute("openid", openID);
        data.put("sessionid", request.getSession().getId());
        map.put("message", "登录成功");
        data.put("user", "{user_name:'ali'}");
        map.put("data", data);
        return map;
    }

}
